package com.lwk.api.action;

import com.lwk.domain.Student;
import com.lwk.service.StudentService;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentAction implements ModelDriven<Student> {
    private Student student = new Student();
    private Integer number;

    public void setNumber(Integer number) {
        this.number = number;
    }

    public void setStudentService(StudentService studentService) {
        this.studentService1 = studentService;
    }

    private StudentService studentService1;

    /**
     * 学生登录
     * @return
     * @throws IOException
     */
    public String login() throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        DetachedCriteria dc = DetachedCriteria.forClass(Student.class);
        dc.add(Restrictions.eq("studentNumber", student.getStudentNumber()));
        dc.add(Restrictions.eq("password", student.getPassword()));
        List<Student> list = studentService1.login(dc);
        if (list.size() != 0) {
            Student student1 = list.get(0);
            map.put("status", "1");
            map.put("student", student1);
            // JSONArray,JSONObject
            JSONObject jsonObject = JSONObject.fromObject(map);
            System.out.println(jsonObject.toString());

		/*
         * config.setExcludes(new String[]{"Student","saleVisits"}); String json =
		 * JSONArray.fromObject(pb.getList()).toString(); System.out.println(json);
		 */

            ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
            ServletActionContext.getResponse().getWriter().write(jsonObject.toString());
        } else {
            map.put("status", "0");
            // JSONArray,JSONObject
            JSONObject jsonObject = JSONObject.fromObject(map);
            System.out.println(jsonObject.toString());

		/*
		 * config.setExcludes(new String[]{"Student","saleVisits"}); String json =
		 * JSONArray.fromObject(pb.getList()).toString(); System.out.println(json);
		 */

            ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
            ServletActionContext.getResponse().getWriter().write(jsonObject.toString());
        }
        return null;
    }

    /**
     * 学生打卡
     * @return
     * @throws IOException
     */
    public String punch() throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println(number);
        DetachedCriteria dc = DetachedCriteria.forClass(Student.class);
        dc.add(Restrictions.eq("studentNumber", student.getStudentNumber()));
        Integer bl = studentService1.getByStudentNumber(dc,number);
        if (bl==1) {
            map.put("msg", "打卡成功");
            System.out.println("打卡成功");
        } else if (bl==3){
            map.put("msg", "已在该机器上打卡");
            System.out.println("已在该机器上打卡");
        } else {
            map.put("msg", "打卡失败");
            System.out.println("打卡失败");
        }
        JSONObject jsonObject = JSONObject.fromObject(map);
        ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
        ServletActionContext.getResponse().getWriter().write(jsonObject.toString());
        return null;
    }

    @Override
    public Student getModel() {
        return student;
    }
}
